package com.ruoyi.web.controller.tledu;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.email.domain.EmailMailbox;
import com.ruoyi.email.service.IEmailMailboxService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 信箱Controller
 * 
 * @author cc
 * @date 2023-03-24
 */
@Controller
@RequestMapping("/eamil/mail")
public class EmailMailboxController extends BaseController
{
    private String prefix = "eamil/mail";

    @Autowired
    private IEmailMailboxService emailMailboxService;

    @RequiresPermissions("eamil:mail:view")
    @GetMapping()
    public String mail()
    {
        return prefix + "/mail";
    }

    /**
     * 查询信箱列表
     */
    @RequiresPermissions("eamil:mail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EmailMailbox emailMailbox)
    {
        startPage();
        List<EmailMailbox> list = emailMailboxService.selectEmailMailboxList(emailMailbox);
        return getDataTable(list);

    }

    /**
     * 发信箱
     */
    @RequiresPermissions("eamil:mail:send")
    @PostMapping("/send")
    @ResponseBody
    public TableDataInfo list1(EmailMailbox emailMailbox){
        startPage();
        SysUser user= ShiroUtils.getSysUser();
        String email=user.getEmail();
        List<EmailMailbox> list1=emailMailboxService.selectByEmail(email);
        return getDataTable(list1);
    }


    /**
     * 导出信箱列表
     */
    @RequiresPermissions("eamil:mail:export")
    @Log(title = "信箱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EmailMailbox emailMailbox)
    {
        List<EmailMailbox> list = emailMailboxService.selectEmailMailboxList(emailMailbox);
        ExcelUtil<EmailMailbox> util = new ExcelUtil<EmailMailbox>(EmailMailbox.class);
        return util.exportExcel(list, "信箱数据");
    }

    /**
     * 新增信箱
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存信箱
     */
    @RequiresPermissions("eamil:mail:add")
    @Log(title = "信箱", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EmailMailbox emailMailbox)
    {
        return toAjax(emailMailboxService.insertEmailMailbox(emailMailbox));
    }

    /**
     * 修改信箱
     */
    @RequiresPermissions("eamil:mail:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EmailMailbox emailMailbox = emailMailboxService.selectEmailMailboxById(id);
        mmap.put("emailMailbox", emailMailbox);
        return prefix + "/edit";
    }

    /**
     * 修改保存信箱
     */
    @RequiresPermissions("eamil:mail:edit")
    @Log(title = "信箱", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EmailMailbox emailMailbox)
    {
        return toAjax(emailMailboxService.updateEmailMailbox(emailMailbox));
    }

    /**
     * 删除信箱
     */
    @RequiresPermissions("eamil:mail:remove")
    @Log(title = "信箱", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(emailMailboxService.deleteEmailMailboxByIds(ids));
    }
}
